package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.PermissionService;
import com.itheima.service.RoleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author yangzhen
 * @date 2020-01-12 15:46
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 回显用户表单
     * @return
     */
    @RequestMapping("/findAllRole")
    public Result findAllRole() {
        try {
            List<Role> roleList = roleService.findAll();
            return new Result(true, "", roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "", "服务器正忙");
        }
    }

    /**
     * 分页查询角色
     * @param queryPageBean
     * @return
     */
    @RequestMapping("findRoleByPage")
    public PageResult findRoleByPage(@RequestBody QueryPageBean queryPageBean){
        return   roleService.findRoleByPage(queryPageBean);
    }

    /**
     * 新增角色
     * @param role
     * @param permissionIds
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, @Param("permissionIds") Integer[] permissionIds){
        try {
            Result result = roleService.add(role,permissionIds);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL,MessageConstant.DATA_EXCEPTION);
        }
    }

    /**
     * 编辑回显角色信息
     * @param id
     * @return
     */
    @RequestMapping("/findRoleById")
    public Result findRoleById(Integer id){
        try {
            Map map = roleService.findRoleById(id);
            return new Result(true,"",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.DATA_EXCEPTION);
        }
    }

    /**
     * 编辑角色
     * @param role
     * @param permissionIds
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds){
        try {
            roleService.edit(role,permissionIds);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL,MessageConstant.DATA_EXCEPTION);
        }
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            Result result = roleService.dele(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DEL_ROLE_FAIL,MessageConstant.DATA_EXCEPTION);
        }
    }
}
